using System.Text;
using System.Web.UI;

namespace Controls
{
    /// <summary>
    /// This class contains some utility functions, used across maultiple classes.
    /// </summary>
    public class Common
    {
        /// <summary>
        /// Registers javascript function for a given control, to locate the control
        /// at the client side through dom.
        /// </summary>
        /// <param name="targetPage"><see cref="Page"/> in which the function to get registered.</param>
        /// <param name="targetControl"><see cref="Control"/> whose client ID would be retrived through this function.</param>
        /// <param name="functionName">Name of the function.</param>
        public static void RegisterJSFunctionForClientID(Page targetPage, Control targetControl, string functionName)
        {
            StringBuilder script = new StringBuilder();
            script.Append("<script type='text/javascript'>");
            script.Append("function ");
            script.Append(functionName);
            script.Append("{return '");
            script.Append(targetControl.ClientID);
            script.Append("';}");
            script.Append("</script>");
            if (!targetPage.ClientScript.IsClientScriptBlockRegistered(targetControl.ID))
            {
                targetPage.ClientScript.RegisterClientScriptBlock(targetPage.GetType(), targetControl.ID, script.ToString());
            }
        }
    }
}
